home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 27 (1992-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / Random / chisqr.c < prev    next >
Text File  |  1992-03-30  |  1KB  |  33 lines

  1.  /* This is the chi-squared test for whether a sequence is as well
  2. distributed as a random sequence. The return result should be within
  3. 2*sqrt(R) of R, provided N is at least 10*R. However, test your generator
  4. with a few different seeds; the test is wrong about 1 time in 10.
  5.   * N is the number of random numbers to generate.
  6.   * R is the range of random numbers; integers in the range 0 to R-1 will
  7. be generated. Note that this parameter also controls how much memory will
  8. be used (4*R bytes).
  9.   * seed is the initial seed for the generator.
  10.   Code & comments generated 14 Feb 92 by Peter Thompson almost verbatim
  11. from "Algorithms in C", Robert Sedgewick, pub. 1990 by Addison-Wesley. It a
  12. good book, but not as detailed as where HE got the algorithm from : "The
  13. Art Of Computer Programming", D.E. Knuth, I think volume 2.
  14.  */
  15.  
  16. float chisqr(long N, long R, long seed)
  17. {    /* chisqr test */
  18.   long i;
  19.   long *counts;
  20.   double sum = 0;
  21.  
  22.   counts = (long *) calloc(R,sizeof(long int));
  23.   if counts {
  24.     randinit(seed);
  25.     for (i = N; (i > 0); i--)
  26.       counts[randomint(R)]++;
  27.     for (i = R-1; (i >= 0); i--)
  28.       sum += counts[i]*counts[i];
  29.     free(counts);
  30.   }
  31.   return (float)(R*(sum/(double)N)-N);
  32. }    /* chisqr test */
  33.